package com.moco.launcher.activity;

import u.aly.s;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;

import com.moco.launcher.LauncherApplication;
import com.moco.launcher.R;
import com.oazon.common.util.LightnessControl;
import com.oazon.common.util.Utils;
import com.oazon.common.Logger;

public class GestureStatues extends Activity{
	private static final String TAG = "GestureStatues";
	
	private ImageView mImgViewExit = null;
	private ImageView mImgViewHelp = null;
	private  static LauncherApplication mApp = null;
	private CheckBox mCheckbox = null;
	private CheckBox light_checkbox;
	Activity gestureStatuesActivity;
	private LightnessControl lightNessControl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.gesturestatues_activity);
		gestureStatuesActivity = this;
		lightNessControl = new LightnessControl();
		mApp = (LauncherApplication) this.getApplicationContext();
		mCheckbox = (CheckBox)findViewById(R.id.checkbox);
		light_checkbox = (CheckBox) findViewById(R.id.light_checkbox);
		mImgViewExit = (ImageView)findViewById(R.id.gester_exit_gesture);
		mImgViewHelp = (ImageView)findViewById(R.id.gester_help);
		
		mCheckbox.setChecked(mApp.getOpenSwitchSongGesture());
	//	light_checkbox.setChecked(readIsLightAuto());
		light_checkbox.setChecked(false);
		lightNessControl.SetLightness(gestureStatuesActivity, 255);
		setupListeners();
	}
	
	private void setupListeners() {
		mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean ischecked) {
				Logger.d(TAG, "ischecked=="+ischecked);
				if(ischecked){
					setGestureSharedPreferences(true);
				}else{
					setGestureSharedPreferences(false);
				}
			}
		});
		light_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					lightNessControl.startAutoBrightness(gestureStatuesActivity);
					writeIsLightAuto(true);
				}else{
					
					lightNessControl.stopAutoBrightness(gestureStatuesActivity);
					lightNessControl.SetLightness(gestureStatuesActivity, 255);
					writeIsLightAuto(false);
				}
			}
		});
		mImgViewExit.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		mImgViewHelp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent  intent = new Intent(GestureStatues.this, GestureHelpActivity.class);
				startActivity(intent);
			}
		});
	}
	
	
    public Boolean readIsLightAuto(){
    	Boolean isAuto;
    	SharedPreferences pre = getSharedPreferences("LightCheck", MODE_WORLD_READABLE);
        return isAuto = pre.getBoolean("isAuto", true);
    	}

    public	void writeIsLightAuto(Boolean isAuto){
    	SharedPreferences.Editor editor = getSharedPreferences("LightCheck", MODE_WORLD_WRITEABLE).edit();
        editor.putBoolean("isAuto", isAuto);
        editor.commit();
       // Log.e(TAG, false+"isFirstRun");
    	}
	
	public void setGestureSharedPreferences(Boolean value) {
		final SharedPreferences mshPreferences = this.getSharedPreferences(Utils.CONFIG_FILE, MODE_PRIVATE);
		Boolean temp = mshPreferences.getBoolean("gesture_value", false);
		Logger.d(TAG, "gesture_value==" + temp);
		if(value != temp){
			Editor mEditor = mshPreferences.edit();
			mEditor.putBoolean("gesture_value", value);
			mEditor.commit();
			Logger.d(TAG,"Late_gesture_value=="+mshPreferences.getBoolean("gesture_value", false));
			mApp.setOpenSwitchSongGesture(value);
		}
	}

}
